import 'package:built_collection/built_collection.dart';
import 'package:mobile_app.core/core/domain/entities/learning_content/lexical_item/lexical_item.dart';

abstract class GetTrainingCount {
  int call({
    required BuiltMap<String, LexicalItem> lexicalItemMap,
  });
}

class GetTrainingCountImpl implements GetTrainingCount {
  const GetTrainingCountImpl();

  @override
  int call({
    required BuiltMap<String, LexicalItem> lexicalItemMap,
  }) {
    final trainingEndTimes = <DateTime>{};

    for (final lexicalItem in lexicalItemMap.values) {
      final progress = lexicalItem.progress;

      if (progress is! LearningLexicalItemProgress) {
        continue;
      }

      final learningStages = progress.learningStages;

      for (var i = 1; i < learningStages.length; i++) {
        final currentLearningStage = learningStages[i];
        final previousLearningStage = learningStages[i - 1];

        if (currentLearningStage is RepeatableLearningStage &&
            previousLearningStage is SuspendedLearningStage &&
            currentLearningStage.repetitionNumber == previousLearningStage.repetitionNumber &&
            currentLearningStage.learningEvent == previousLearningStage.learningEvent) {
          trainingEndTimes.add(currentLearningStage.time);
        }
      }
    }

    return trainingEndTimes.length;
  }
}
